import { POST } from '../axios.config.js'
import { encryption } from '@utility/crypto.js'
const base = window.config.ip

// 登录密码
export const loginApiPass = obj => {
  const basicAuth = 'Basic ' + window.btoa('pig:pig')
  const str = `?username=${obj.loginName}&randomStr=blockPuzzle&code=${obj.smCode}&grant_type=password&scope=server`
  const encPassword = encryption(obj.loginPassword, 'pigxpigxpigxpigx')
  const passData = {
    password: encPassword,
  }
  // 使用 URLSearchParams 序列化数据
  const urlEncodedData = new URLSearchParams(passData)
  const data = {
    str: str,
    param: urlEncodedData.toString(),
    basicAuth,
  }

  return POST(`${base}/auth/oauth2/token${data.str}`, data.param, {
    headers: {
      skipToken: true,
      Authorization: data.basicAuth,
      'Content-Type': 'application/x-www-form-urlencoded',
    },
  })
}

// 登录短信
export const loginApiMobile = p =>
  POST(`${base}/auth/oauth2/token${p.str}`, p.param, {
    headers: {
      skipToken: true,
      Authorization: p.basicAuth,
      'Content-Type': 'application/x-www-form-urlencoded',
    },
  })
